package svc

import (
	"log"
	"server-go/api/reward/internal/config"
	"server-go/rpc/reward/rewardclient"

	"github.com/apache/rocketmq-client-go/v2"
	"github.com/apache/rocketmq-client-go/v2/producer"
	"github.com/apache/rocketmq-client-go/v2/rlog"
	"github.com/zeromicro/go-zero/zrpc"
)

type ServiceContext struct {
	Config    config.Config
	RewardRpc rewardclient.Reward
	ProducerA rocketmq.Producer
}

func NewServiceContext(c config.Config) *ServiceContext {
	// 初始化，发送验证码的生产者
	producerA, err := rocketmq.NewProducer(producer.WithNameServer([]string{c.Rocketmq.Host}), producer.WithRetry(2))
	if err != nil {
		log.Println(c.Rocketmq.TopicA + "生产者初始化失败")
		panic(c.Rocketmq.TopicA + "生产者初始化失败")
	}
	// rocketmq 日志级别
	rlog.SetLogLevel("warn")

	return &ServiceContext{
		Config:    c,
		RewardRpc: rewardclient.NewReward(zrpc.MustNewClient(c.RewardRpc)),
		ProducerA: producerA,
	}
}
